var breadcrumb = $('#breadcrumb');
var menuList = $('#menuList');
var pageContents = $('#pageContents');
$(function() {
	$('#logoutBtn').on('click', function() {
		window.location.href = contextPath + '/login/out';
	});
	$('#modifyPasswordBtn').off().on('click', function() {
		$.get(contextPath + '/bgUser/modifyPasswordTemplate').done(function(html) {
			var dialog = $(html).modal({
				keyboard: false
			}).on({
				'hidden.bs.modal': function(){
					$(this).remove();
				}
			});
			seajs.use('modifyPassword', function(modifyPassword){
				modifyPassword.init(dialog);
			});
		});
	});
	$.get(contextPath+'/bgUser/checkPassword/'+userId).done(function(data){
		if (data.statusCode == 20011011) {
			$('#modifyPasswordBtn').click();
		}
	});
	$.get(contextPath + '/bgMenu/topMenu').done(function(data) {
		$.each(data.dataList, function() {
			if (this.type == 'item') {
				var $li = $('<li data-role="' + this.type + '" data-title="' + this.menuName + '"  data-action="' + this.actionUrl + '">');
				$li.html('<a href="#"><i class="' + this.icon + '"></i><span class="menu-text"> ' + this.menuName + ' </span></a>');
				$li.on('click', function() {
					openPage($(this))
				});
				menuList.append($li);
			} else {
				var $li = $('<li data-role="' + this.type + '" data-title="' + this.menuName + '" data-id="' + this.id + '" data-loader="0">');
				$li.html('<a href="#" class="dropdown-toggle"><i class="' + this.icon + '"></i>' + '<span class="menu-text">&nbsp;' + this.menuName + '</span><b class="arrow fa fa-angle-down"></b></a>' + '<ul class="submenu"></ul>');
				$li.on('click', function() {
					var $this = $(this);
					if ($this.data('loaded') == 1) {
						return;
					}
					$this.data('loaded', 1);
					var id = $this.data('id');
					$.get(contextPath + '/bgMenu/menuByParentId/' + id).done(function(data) {
						loadMenu($li.find('.submenu'), data);
					});
				});
				menuList.append($li);
			}
		});
		$('#menuList [data-title="设备报修"]').click();
	});

	var loadMenu = function($element, data) {
		$.each(data.dataList, function() {
			if (this.type == 'item') {
				var $li = $('<li data-role="' + this.type + '" data-title="' + this.menuName + '"  data-action="' + this.actionUrl + '">');
				$li.html('<a href="#"><i class="fa fa-angle-double-right"></i>' + this.menuName + '</a>');
				$element.append($li);
				$li.on('click', function() {
					openPage($(this))
				});
			} else {
				var $li = $('<li data-role="' + this.type + '" data-id="' + this.id + '" data-loader="0">');
				$li.html('<a href="#" class="dropdown-toggle"><i class="fa fa-desktop"></i>' + '<span class="menu-text">' + this.menuName + ' <span><b class="arrow fa fa-angle-down"></b></a>' + '<ul class="submenu"></ul>');
				$li.on('click', function() {
					var $this = $(this);
					if ($this.data('loaded') == 1) {
						return;
					}
					$this.data('loaded', 1);
					var id = $this.data('id');
					$.get(contextPath + '/bgMenu/menuByParentId/' + id).done(function(data) {
						loadMenu($li.find('.submenu'), data);
					});
				});
				$element.append($li);
			}
		});
	}
	$.get(contextPath + '/welcome.html').done(function(html) {
		pageContents.html(html);
	});
	$('#navigation [data-role="count"]').on('click', function(){
		var status = $(this).data('value');
		var repairLi = $('#menuList [data-title="设备报修"]');
		openPage(repairLi, status);
	});
	$('#allRepair').off().click('click', function(){
		$('#menuList [data-title="设备报修"]').click();
	});
	getRepairCountByStatus();
	setInterval(getRepairCountByStatus, 300000);
});

$.ajaxSetup({
	error : function(XMLHttpRequest, textStatus) {
		if (XMLHttpRequest.status == 499) {
			window.location.href = contextPath + '/login/out';
		} else {
			if (XMLHttpRequest.status == 404 || XMLHttpRequest.status == 500) {
				$.get(contextPath + '/errors/' + XMLHttpRequest.status + '.html').done(function(html) {
					$('#pageContents').html(html);
				});
			} else {
				$.get(contextPath + '/errors/404.html').done(function(html) {
					$('#pageContents').html(html);
				});
			}
		}
	}
});

function getRepairCountByStatus(){
	$.get(contextPath + '/repair/getCountByStatus').done(function(data){
		for(var i=0,j=data.length; i<j; i++){
			var count = data[i][0];
			var status = data[i][1];
			if(status == 0){
				$('#totalCount').text(count);
			}
			$('#navigation [data-role="count"][data-value="'+status+'"]').find('[data-role="value"]').text('+'+count);
		}
	}).fail(function(){
		window.location.href = contextPath + '/login/out';
	});
}

function addBreadcrumbMenu(title) {
	var last = breadcrumb.find('li:last-child');
	var data = last.data();
	last.html('<a href="#">' + data.title + '</a>').on('click', function() {
		menuList.find('[data-title="' + data.title + '"]').click();
	});
	breadcrumb.append($('<li>').html(title));
}
function openPage($this, param) {
		breadcrumb.empty();
		$('<li>').html('<i class="fa fa-home home-icon"></i><a href="#">首页</a>').appendTo(breadcrumb).on('click', function() {
			$(this).nextAll('li').remove();
			$.get(contextPath + '/welcome.html').done(function(html) {
				pageContents.html(html);
			});
		});
		menuList.find('.active').removeClass('active');
		$this.addClass('active').parents('li').addClass('active').each(function() {
			var data = $(this).data();
			var $li = $('<li>').html('<a href="#">' + data.title + '</a>').data(data);
			breadcrumb.append($li)
			if (data == 'menu') {
				$li.on('click', function() {
					menuList.find('[data-title="' + data.title + '"]').click();
				});
			}
		});
		var data = $this.data();
		$('<li>' + data.title + '</li>').appendTo(breadcrumb).data(data);
		loadPage(data, param);
};
function loadPage(data, param) {
	var url = contextPath + '/' + data.action;
	if(param != null ){
		url += '?param='+param;
	}
	$.get(url).done(function(html) {
		pageContents.html(html);
	});
};

function getTimeStr(time){
    if(!time){
        return '';
    }
    var date = new Date();
    date.setTime(time);
    var year = date.getFullYear();
    var moth = (date.getMonth()+1<10) ? '0' + (date.getMonth()+1) : date.getMonth()+1;
    var day = (date.getDay()+1<10) ? '0' + (date.getDay()+1) : date.getDay()+1;
    var hour = (date.getHours()+1<10) ? '0' + (date.getHours()+1) : date.getHours()+1;
    var minutes = (date.getMinutes()+1<10) ? '0' + (date.getMinutes()+1) : date.getMinutes()+1;
    var second = (date.getSeconds()+1<10) ? '0' + (date.getSeconds()+1) : date.getSeconds()+1;
    return year+'-'+moth+'-'+day+' '+hour+':'+minutes+':'+second;
};
